#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)) )
    {
        return false;
    }
    
	this->CurrentMap = dkm(1, this);
	this->drawMap(this->CurrentMap);
	this->CurrentMap->startActors();
	
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 20);

    /////////////////////////////
    // 3. add your codes below...
	//CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	//this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );
	this->setTouchEnabled(true);

    
    return true;
}



void HelloWorld::drawMap(DKMap *map)
{
	CCObject *obj = NULL;
	CCARRAY_FOREACH(map->Actors, obj)
	{
		DKActor * actor = dynamic_cast<DKActor *>(obj);
	    // add "HelloWorld" splash screen"


    // position the sprite on the center of the screen

    // add the sprite as a child to this layer
    this->addChild(actor, actor->zIndex);
	}
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
	if(touches->count() > 1) return; //Don't allow multitouch

    // Choose one of the touches to work with
    CCTouch* touch = (CCTouch*)( touches->anyObject());
    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);

	this->CurrentMap->ProcessUserInput(location);
}

void HelloWorld::addActor( DKActor *actor )
{
	this->addChild(actor, actor->zIndex);
}
